import { useState } from "react";
import { fetchContractData } from "../contracts/fetchContractData";
import { useTonWallet } from "@tonconnect/ui-react";

const CallSmartContract: React.FC = () => {
    const wallet:any = useTonWallet();
    // console.log(wallet)
    const [contractData, setContractData] = useState<any>(null);
    const contractAddress = "EQBYLTm4nsvoqJRvs_L-IGNKwWs5RKe19HBK_lFadf19FUfb"; // 替换成你的合约地址

    const handleFetchData = async () => {
        try {
            const data = await fetchContractData(contractAddress,wallet.account.chain);
            console.log('data---')
            console.log(data)
            setContractData(data);
        } catch (error) {
            console.error("获取合约失败:", error);
        }
    };

    return (
        <div>
            <button onClick={handleFetchData}>查询合约</button>
            {contractData && <pre>{JSON.stringify(contractData, null, 2)}</pre>}
        </div>
    );
};

export default CallSmartContract;
